﻿#include "dtusettingwidget.h"

DTUSettingWidget::DTUSettingWidget()
{
    setup_ui();
    connect(settingButton, &QPushButton::clicked, this, &DTUSettingWidget::showSetting);
    connect(cancelButton,&QPushButton::clicked, this, &DTUSettingWidget::cancelSetting);
}

DTUSettingWidget::~DTUSettingWidget()
{

}

void DTUSettingWidget::showSetting()
{
    if(name_ed->text().isEmpty() || pos_ed->text().isEmpty())
        QMessageBox::warning(NULL, QStringLiteral("请选择设备"), QStringLiteral("未选择设备"));
    else
    {
        okButton->setVisible(true);
        cancelButton->setVisible(true);
        settingButton->setVisible(false);

        name_ed->setEnabled(true);
        pos_ed->setEnabled(true);
    }
}

bool DTUSettingWidget::checkSetting()
{
    if(name_ed->text().isEmpty() || pos_ed->text().isEmpty())
    {
        QMessageBox::warning(NULL, QStringLiteral("设置错误"), QStringLiteral("设备设置不能为空"));
        return false;
    }
    else
    {
        okButton->setVisible(false);
        cancelButton->setVisible(false);
        settingButton->setVisible(true);

        name_ed->setEnabled(false);
        pos_ed->setEnabled(false);
        return true;
    }

}

void DTUSettingWidget::cancelSetting()
{
    flush();
    okButton->setVisible(false);
    cancelButton->setVisible(false);
    settingButton->setVisible(true);

    name_ed->setEnabled(false);
    pos_ed->setEnabled(false);
}

void DTUSettingWidget::flush()
{
    name_ed->clear();
    pos_ed->clear();
}

void DTUSettingWidget::setup_ui()
{
    QVBoxLayout* layout = new QVBoxLayout(this);
    QLabel* dtuSettingTitle = new QLabel(QStringLiteral("DTU设置"));
    dtuSettingTitle->setStyleSheet("background-color:#d4d0c8;");
    dtuSettingTitle->setAlignment(Qt::AlignCenter);
    //dtuSettingTitle->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    layout->addWidget(dtuSettingTitle);

    QGridLayout* gridlay = new QGridLayout();
    QLabel* name_l = new QLabel(QStringLiteral("名称"));
    name_l->setAlignment(Qt::AlignCenter);
    name_l->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    QLabel* pos_l = new QLabel(QStringLiteral("位置"));
    pos_l->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    pos_l->setAlignment(Qt::AlignCenter);
    name_ed = new QLineEdit();
    name_ed->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    name_ed->setEnabled(false);
    pos_ed = new QLineEdit();
    pos_ed->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    pos_ed->setEnabled(false);

    gridlay->addWidget(name_l,0,0,1,2);
    gridlay->addWidget(name_ed,0,2,1,4);
    gridlay->addWidget(pos_l,1,0,1,2);
    gridlay->addWidget(pos_ed,1,2,1,4);

    okButton = new QPushButton(QStringLiteral("确认"));
    okButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    cancelButton = new QPushButton(QStringLiteral("取消"));
    cancelButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    settingButton = new QPushButton(QStringLiteral("设置"));
    settingButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    gridlay->addWidget(settingButton,2,0,1,2);
    gridlay->addWidget(okButton,2,2,1,2);
    gridlay->addWidget(cancelButton, 2,4,1,2);
    okButton->setVisible(false);
    cancelButton->setVisible(false);

    gridlay->setSpacing(5);
    gridlay->setMargin(20);

    layout->addWidget(dtuSettingTitle);
    layout->addLayout(gridlay);
    //layout->setStretchFactor(dtuSettingTitle,1);
    //layout->setStretchFactor(gridlay,5);

    layout->setMargin(0);
    layout->setSpacing(1);
    setLayout(layout);
    //setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
}

void DTUSettingWidget::setInfo(const DTUData &data)
{
    name_ed->setText(data.name);
    pos_ed->setText(data.position);
}
